import 'package:flutter/material.dart';

import '../../flutter_widgets.dart';
import 'models/assets.dart';

/// 空态页，支持设置空态图片，文字，展示的位置alignment
///
/// {@tool src.sample}
/// ```dart
/// EmptyView(
///        child: Center(child: Text('content')),
///        isEmpty: _isEmpty,
///        emptyText: '啥数据也没有～',
///        emptyStyle: EmptyStyle(alignment: Alignment(0, 0)),
///      )
/// ```
/// @{end-tool}
class EmptyView extends StatelessWidget {
  final Widget child;
  final bool isEmpty;
  final String? emptyText;
  final EmptyStyle? emptyStyle;

  const EmptyView({
    super.key,
    required this.child,
    required this.isEmpty,
    this.emptyText,
    this.emptyStyle,
  });

  @override
  Widget build(BuildContext context) {
    final emptyStyle = this.emptyStyle ?? WidgetTheme.of(context)?.emptyStyle;
    return isEmpty
        ? Align(
            alignment: emptyStyle?.alignment ?? const Alignment(0, -0.5),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                CommonImage.asset(
                  emptyStyle?.imageAsset ?? Images.empty,
                  width: emptyStyle?.imageSize ?? 140,
                ),
                Padding(
                  padding:
                      EdgeInsets.only(top: emptyStyle?.drawablePadding ?? 16),
                  child: Text(
                    emptyText ?? '暂无数据',
                    style: emptyStyle?.textStyle ??
                        Theme.of(context).textTheme.bodySmall,
                  ),
                )
              ],
            ),
          )
        : child;
  }
}

class EmptyStyle {
  final String imageAsset;
  final double imageSize;
  final Alignment alignment;
  final double drawablePadding;
  final TextStyle? textStyle;

  const EmptyStyle({
    this.imageAsset = Images.empty,
    this.imageSize = 140.0,
    this.alignment = const Alignment(0, -0.5),
    this.drawablePadding = 16,
    this.textStyle,
  });
}
